﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using SuperMap.Data;
using Rail.Start;
using SuperMap.Mapping;
using SuperMap.Realspace;
using Rail.Start.Viewer;
using SuperMap.UI;

namespace SuperMap.Sample.Data
{
    public class SampleRun
    {
        public Workspace m_workspace;
        private TreeView m_workspaceTree;
        public WorkspaceConnectionInfo m_connectionInfo;
        public SceneExtend m_scenecontrol;

        private String m_opensu = "";
        private String m_openfa = "";
        private String m_createsu = "";
        private String m_createfa = "";
        private String m_saveassu = "";
        private String m_saveasfa = "";
        private String m_deletesu = "";
        private String m_deletefa = "";
        private String m_workspaceinfo1 = "";
        private String m_workspaceinfo2 = "";
        private String m_workspaceinfo3 = "";
        private String m_workspaceinfo4 = "";
        private String m_workspaceinfo5 = "";
        private String m_workspaceinfo6 = "";
        private String m_workspaceinfo7 = "";
        private String m_workspaceinfo8 = "";
        private String m_workspaceinfo9 = "";
        private String m_workspaceinfo10 = "";
        private String m_workspaceinfo11 = "";
        private String m_workspaceinfo12 = "";
        private String m_workspaceinfo13 = "";

        public delegate void CheckEventHandler(Boolean isInitialize);
        public event CheckEventHandler OnCheck;


        /// <summary>
        /// 根据workspace构造 SampleRun对象
		/// Initialize the SampleRun object with the specified workspace
        /// </summary>
        public SampleRun(Workspace workspace)
        {

            try
            {
                m_workspace = workspace;
                InitializeCultureResources();
                m_scenecontrol = new SceneExtend()
                {
                    Dock = System.Windows.Forms.DockStyle.Fill
                };

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        private void InitializeCultureResources()
        {
            if (SuperMap.Data.Environment.CurrentCulture == "zh-CN")
            {
                m_opensu = "工程打开成功！";
                m_openfa = "工程打开失败！";
                m_createsu = "工程创建成功！";
                m_createfa = "工程创建失败！";
                m_saveassu = "工程另存成功！";
                m_saveasfa = "工程另存失败！";
                m_deletesu = "工程删除成功！";
                m_deletefa = "工程删除失败！";
                m_workspaceinfo1 = "工作空间基本信息：";
                m_workspaceinfo2 = "服务器名或文件路径：";
                m_workspaceinfo3 = "数据库名：";
                m_workspaceinfo4 = "用户名：";
                m_workspaceinfo5 = "工程名称：";
                m_workspaceinfo6 = "工程类型：";
                m_workspaceinfo7 = "工程版本：";
                m_workspaceinfo8 = "工程中数据源信息：";
                m_workspaceinfo9 = "数据源编号";
                m_workspaceinfo10 = "工作空间中没有数据源！";
                m_workspaceinfo11 = "工作空间中地图信息：";
                m_workspaceinfo12 = "地图编号";
                m_workspaceinfo13 = "工作空间中没有地图！";
            }
            else
            {
                m_opensu = "Success to open the workspace!";
                m_openfa = "Fail to open the workspace!";
                m_createsu = "Success to create the workspace!";
                m_createfa = "Fail to create the workspace!";
                m_saveassu = "Success to save the current workspace as another workspace!";
                m_saveasfa = "Fail to save the current workspace as another workspace!";
                m_deletesu = "Success to delete the current workspace!";
                m_deletefa = "Fail to delete the workspace!";
                m_workspaceinfo1 = "Basic information of the workspace:";
                m_workspaceinfo2 = "Name of the database or path of the file:";
                m_workspaceinfo3 = "Name of the database:";
                m_workspaceinfo4 = "User name:";
                m_workspaceinfo5 = "Name of the workspace:";
                m_workspaceinfo6 = "Type of the workspace:";
                m_workspaceinfo7 = "Version of the workspace:";
                m_workspaceinfo8 = "Datasource information of the workspace:";
                m_workspaceinfo9 = "Namber of the datasource";
                m_workspaceinfo10 = "There is no datasource in the workspace!";
                m_workspaceinfo11 = "Information of the map in the workspace:";
                m_workspaceinfo12 = "Number of the map";
                m_workspaceinfo13 = "There is no map in the workspace!";
            }
        }
        #region
        //执行所需要的数据
        //Data
        private string m_datasourcesNode = "";
        private string m_mapsNode = "";
        private string m_scenesNode = "";
        private string m_layoutsNode = "";
        private string m_resourcesNode = "";
        private string m_pointResNode = "";
        private string m_lineResNode = "";
        private string m_fillResNode = "";
        private string m_datasetinfo1 = "";
        private string m_datasetinfo2 = "";
        private string m_datasetinfo3 = "";
        private string m_datasetinfo4 = "";
        private string m_datasetinfo5 = "";
        private string m_datasetinfo6 = "";
        private string m_datasetinfo7 = "";
        private string m_datasetinfo8 = "";
        private string m_datasetinfo9 = "";
        private string m_mapinfo1 = "";
        private string m_mapinfo2 = "";
        private string m_mapinfo3 = "";
        private string m_mapinfo4 = "";
        private string m_mapinfo5 = "";
        private string m_mapinfo6 = "";
        private string m_mapinfo7 = "";
        private string m_mapinfo8 = "";
        private string m_mapinfo9 = "";
        private string m_mapinfo10 = "";
        private string m_mapinfo11 = "";
        private string m_mapinfo12 = "";
        private string m_mapinfo13 = "";
        private string m_mapinfo14 = "";
        private string m_mapinfo15 = "";
        private string m_mapinfo16 = "";
        private string m_mapinfo17 = "";
        private string m_mapinfo18 = "";
        private string m_mapinfo19 = "";
        private string m_mapinfo20 = "";
        private string m_mapinfo21 = "";
        private string m_mapinfo22 = "";
        private string m_mapinfo23 = "";
        private string m_mapinfo24 = "";
        private string m_mapinfo25 = "";
        private string m_mapinfo26 = "";
        private string m_mapinfo27 = "";
        private string m_mapinfo28 = "";
        private string m_layoutinfo1 = "";
        private string m_layoutinfo2 = "";
        private string m_layoutinfo3 = "";
        private string m_layoutinfo4 = "";
        private string m_layoutinfo5 = "";
        private string m_layoutinfo6 = "";
        private string m_layoutinfo7 = "";
        private string m_resourceinfo = "";
        #endregion
        private void InitializeultureResources()
        {
            if (SuperMap.Data.Environment.CurrentCulture == "zh-CN")
            {
                m_datasourcesNode = "数据源";
                m_mapsNode = "地图";
                m_scenesNode = "三维场景";
                m_layoutsNode = "布局";
                m_resourcesNode = "资源";
                m_pointResNode = "点符号库";
                m_lineResNode = "线符号库";
                m_fillResNode = "填充符号库";
                m_datasetinfo1 = "所属数据源：";
                m_datasetinfo2 = "数据集名：";
                m_datasetinfo3 = "描述信息：";
                m_datasetinfo4 = "数据表表：";
                m_datasetinfo5 = "类型：";
                m_datasetinfo6 = "最小外界矩形：";
                m_datasetinfo7 = "编码方式：";
                m_datasetinfo8 = "是否已经打开：";
                m_datasetinfo9 = "是否只读：";
                m_mapinfo1 = "关联的工作空间：";
                m_mapinfo2 = "地图名：";
                m_mapinfo3 = "描述信息：";
                m_mapinfo4 = "空间范围：";
                m_mapinfo5 = "中心点：";
                m_mapinfo6 = "颜色模式：";
                m_mapinfo7 = "坐标单位：";
                m_mapinfo8 = "比例尺：";
                m_mapinfo9 = "是否反走样地图：";
                m_mapinfo10 = "自定义地图边界是否有效：";
                m_mapinfo11 = "是否动态投影显示：";
                m_mapinfo12 = "是否被修改：";
                m_mapinfo13 = "是否绘制地图背景：";
                m_mapinfo14 = "文本角度是否固定：";
                m_mapinfo15 = "图层信息：";
                m_mapinfo16 = "图层总数：";
                m_mapinfo17 = "图层编号{0}:{1}\n";
                m_mapinfo18 = "图层名：";
                m_mapinfo19 = "对应数据集：";
                m_mapinfo20 = "过滤条件：";
                m_mapinfo21 = "不透明度：";
                m_mapinfo22 = "制图表达字段：";
                m_mapinfo23 = "专题图";
                m_mapinfo24 = "是否可见：";
                m_mapinfo25 = "图层是否可编辑：";
                m_mapinfo26 = "是否采用制图表达：";
                m_mapinfo27 = "图层中对象是否可选择：";
                m_mapinfo28 = "是否可捕捉：";
                m_layoutinfo1 = "关联的工作空间：";
                m_layoutinfo2 = "布局名：";
                m_layoutinfo3 = "空间范围：";
                m_layoutinfo4 = "中心点：";
                m_layoutinfo5 = "布局元素信息：\n";
                m_layoutinfo6 = "布局元素总数：";
                m_layoutinfo7 = "布局元素编号{0}:{1}";
                m_resourceinfo = "符号索引{0}:{1}\n";
            }
            else
            {
                m_datasourcesNode = "Datasources";
                m_mapsNode = "Maps";
                m_scenesNode = "Scenes";
                m_layoutsNode = "Layouts";
                m_resourcesNode = "Resources";
                m_pointResNode = "PointLibaray";
                m_lineResNode = "LineLibaray";
                m_fillResNode = "FillLibaray";
                m_datasetinfo1 = "Datasource:";
                m_datasetinfo2 = "Name:";
                m_datasetinfo3 = "Description:";
                m_datasetinfo4 = "TableName:";
                m_datasetinfo5 = "Type:";
                m_datasetinfo6 = "Bounds:";
                m_datasetinfo7 = "EncodeType:";
                m_datasetinfo8 = "IsOpen:";
                m_datasetinfo9 = "IsReadOnly:";
                m_mapinfo1 = "Workspace:";
                m_mapinfo2 = "Name:";
                m_mapinfo3 = "Description:";
                m_mapinfo4 = "Bounds:";
                m_mapinfo5 = "Center:";
                m_mapinfo6 = "ColorMode:";
                m_mapinfo7 = "CoordUnit:";
                m_mapinfo8 = "Scale:";
                m_mapinfo9 = "IsAntialias:";
                m_mapinfo10 = "IsCustomBoundsEnabled:";
                m_mapinfo11 = "IsDynamicProjection:";
                m_mapinfo12 = "IsModified:";
                m_mapinfo13 = "IsPaintBackground:";
                m_mapinfo14 = "IsTxstAngleFixed:";
                m_mapinfo15 = "LayerInformation:";
                m_mapinfo16 = "Count of layers:";
                m_mapinfo17 = "Number of layer{0}:{1}\n";
                m_mapinfo18 = "Name:";
                m_mapinfo19 = "Dataset:";
                m_mapinfo20 = "DisplayFilter:";
                m_mapinfo21 = "OpaqueRate:";
                m_mapinfo22 = "RepresentationField:";
                m_mapinfo23 = "Theme:";
                m_mapinfo24 = "IsVisible:";
                m_mapinfo25 = "IsEditable:";
                m_mapinfo26 = "IsRepresentationEnabled:";
                m_mapinfo27 = "IsSelectable:";
                m_mapinfo28 = "IsSnapable:";
                m_layoutinfo1 = "Workspace:";
                m_layoutinfo2 = "Name:";
                m_layoutinfo3 = "Bounds:";
                m_layoutinfo4 = "Center:";
                m_layoutinfo5 = "Information of layout:\n";
                m_layoutinfo6 = "Count of the elements:";
                m_layoutinfo7 = "Number of layout {0}:{1}";
                m_resourceinfo = "Index of resource{0}:{1}\n";
            }
        }
        private void SetWorkspaceTree(TreeView workspaceTree, Workspace workspace)
        {
            try
            {

                workspaceTree.Nodes.Clear();
                // 工作空间节点
                // Nodes of the workspace
                TreeNode workspaceNode = this.CreateNode(workspace.Caption, "Workspace", "Workspace");

                TreeNode datasourcesNode = this.CreateNode(m_datasourcesNode, "Datasources", "Datasources");
                this.SetDatasourcesNode(datasourcesNode, workspace.Datasources);

                TreeNode mapsNode = this.CreateNode(m_mapsNode, "Maps", "Maps");
                this.SetMapsNode(mapsNode, workspace.Maps);

                TreeNode scenesNode = this.CreateNode(m_scenesNode, "Scenes", "Scenes");
                this.SetScenesNode(scenesNode, workspace.Scenes);

                TreeNode layoutsNode = this.CreateNode(m_layoutsNode, "Layouts", "Layouts");
                this.SetLayoutsNode(layoutsNode, workspace.Layouts);

                TreeNode resourcesNode = this.CreateNode(m_resourcesNode, "Resources", "Resources");
                this.SetResourcesNode(resourcesNode, workspace.Resources);

                // 添加节点
                // Add the nodes
                workspaceNode.Nodes.Add(datasourcesNode);
                workspaceNode.Nodes.Add(mapsNode);
                workspaceNode.Nodes.Add(scenesNode);
                workspaceNode.Nodes.Add(layoutsNode);
                workspaceNode.Nodes.Add(resourcesNode);
                workspaceNode.Expand();
                workspaceTree.Nodes.Add(workspaceNode);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }


        /// <summary>
        /// 根据数据源连接信息创建数据源，返回相应的描述信息
        /// Create the datasource based on datsource connection information, and return the description information
        /// </summary>
        public String CreateDatasource(DatasourceConnectionInfo datasourceInfo)
        {
            String discription = "";
            try
            {
                m_workspace.Datasources.CloseAll();
                Datasource datasource = m_workspace.Datasources.Create(datasourceInfo);

                if (datasource != null)
                {
                    //成功
                    discription = "创建数据源成功";
                }
                else
                {
                    //失败
                    discription = "创建数据源失败";
                }
            }
            catch (Exception e)
            {
                discription = "创建数据源失败：" + e.Message;
            }

            return discription;
        }
        /// <summary>
        /// 根据数据源连接信息打开数据源，返回相应的描述信息
        /// </summary>
        public String OpenDatasource(DatasourceConnectionInfo datasourceInfo)
        {
            m_workspace.Datasources.CloseAll();
            String discription = "";
            if (m_workspace != null)
            {
                try
                {
                    Datasource datasource = m_workspace.Datasources.Open(datasourceInfo);

                    if (datasource != null)
                    {
                        discription = "数据源打开成功";
                    }
                    else
                    {
                        discription = "数据源打开失败";
                    }
                }
                catch (Exception e)
                {
                    discription = "数据源打开失败：" + e.Message;
                }
            }
            return discription;
        }
        /// <summary>
        /// 打开工作空间
		/// Open the workspace
        /// </summary>
        /// <param name="connectionInfo">工作空间连接信息 WorkspaceConnectionInfo</param>
        /// <returns>操作结果描述信息 The description information of the result</returns>
        public String Open(WorkspaceConnectionInfo connectionInfo)
        {
            String result = String.Empty;

            try
            {
                Close();
                //m_workspace.Close();

                m_connectionInfo = connectionInfo;

                Boolean isSucceed = m_workspace.Open(m_connectionInfo);

                if (isSucceed)
                {
                    result = System.Environment.NewLine;
                    result += m_opensu;
                    OnCheck(false);
                    result += System.Environment.NewLine;
                    // 获取成功后的工作空间信息
                    // Gets the information of the workspace
                    result += GetWorkspaceInfomation();
                    InitializeultureResources();
                    SetWorkspaceTree(FormMain.formMain.WorkspaceTree, m_workspace);
                    OpenOneScene();
                }
                else
                {
                    result = System.Environment.NewLine;
                    result += m_openfa;
                    OnCheck(true);
                    result += System.Environment.NewLine;

                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            return result;
        }
        public String Close()
        {
            String result = String.Empty;
            try
            {
                m_scenecontrol.Scene.Close();
                if (FormMain.formMain.LayersControlpanel.Scene != null)
                    FormMain.formMain.LayersControlpanel.Scene.Close();
                m_workspace.Close();
               // m_workspace.Dispose();
                FormMain.formMain.WorkspaceTree.Nodes.Clear();
                OnCheck(true);
                result += "工程关闭成功！";
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                result += "工程关闭失败！";
            }
            return result;
        }
        /// <summary>
        /// 创建工作空间
        /// Create the workspace
        /// </summary>
        /// <param name="connectionInfo">工作空间连接信息 WorkspaceConnectionInfo</param>
        /// <returns>操作结果描述信息 The description information of the result</returns>
        public String Create(WorkspaceConnectionInfo connectionInfo)
        {
            String result = String.Empty;
            try
            {
                m_workspace.Close();
                m_connectionInfo = connectionInfo;

                Boolean isSucceed = m_workspace.Create(m_connectionInfo);
                if (isSucceed)
                {
                    result = System.Environment.NewLine;
                    result += m_createsu;
                    OnCheck(false);
                    result += System.Environment.NewLine;
                    result += GetWorkspaceInfomation();
                }
                else
                {
                    result = System.Environment.NewLine;
                    result += m_createfa;
                    OnCheck(true);
                    result += System.Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return result;
        }
        public void Save()
        {
            SaveScene();
            //m_workspace.Save();
        }
        /// <summary>
        /// 将三维场景保存到工作空间中
        /// Save the scene to the workspace
        /// </summary>
        public void SaveScene()
        {
            try
            { 
                String sceneXml = m_scenecontrol.Scene.ToXML();
                m_workspace.Scenes.Add(m_scenecontrol.Scene.Name, sceneXml);
                m_workspace.Save();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 另存工作空间
        /// Save the current workspace as another workspace
        /// </summary>
        /// <param name="connectionInfo">工作空间连接信息 WorkspaceConnectionInfo</param>
        /// <returns>操作结果描述信息 The description information of the result</returns>
        public String SaveAs(WorkspaceConnectionInfo connectionInfo)
        {
            String result = String.Empty;
            try
            {
                m_connectionInfo = connectionInfo;

                Boolean isSucceed = m_workspace.SaveAs(m_connectionInfo);


                if (isSucceed)
                {
                    MessageBox.Show("操作成功！");
                    result = System.Environment.NewLine;
                    result += m_saveassu;

                    result += System.Environment.NewLine;
                    result += GetWorkspaceInfomation();
                }
                else
                {
                    result = System.Environment.NewLine;
                    result += m_saveasfa;

                    result += System.Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return result;
        }


        /// <summary>
        /// 删除工作空间
		/// Delete the current workspace
        /// </summary>
        /// <returns>操作结果描述信息 The description information of the result</returns>
        public String Delete()
        {
            String result = String.Empty;

            try
            {
                Boolean isSucceed = Workspace.DeleteWorkspace(m_connectionInfo);

                if (isSucceed)
                {
                    result = System.Environment.NewLine;
                    result += m_deletesu;

                    result += System.Environment.NewLine;
                    result += GetWorkspaceInfomation();
                    OnCheck(true);
                }
                else
                {
                    result = System.Environment.NewLine;
                    result += m_deletefa;

                    result += System.Environment.NewLine;
                    OnCheck(false);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 获取当前的工作空间信息
		/// Gets the information of the current workspace 
        /// </summary>
        /// <returns>工作空间信息 The information of the workspace</returns>
        public String GetWorkspaceInfomation()
        {
            StringBuilder stringBuilder = new StringBuilder();
            try
            {
                stringBuilder.Append(m_workspaceinfo1);
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo2);
                stringBuilder.Append(m_connectionInfo.Server);
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo3);
                stringBuilder.Append(m_connectionInfo.Database);
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo4);
                stringBuilder.Append(m_connectionInfo.User);
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo5);
                stringBuilder.Append(m_connectionInfo.Name);
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo6);
                stringBuilder.Append(m_connectionInfo.Type.ToString());
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo7);
                stringBuilder.Append(m_connectionInfo.Version.ToString());
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append("************************************************************");
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo8);

                Int32 datasourceCount = m_workspace.Datasources.Count;
                if (datasourceCount > 0)
                {
                    for (int i = 0; i < datasourceCount; i++)
                    {
                        stringBuilder.Append(m_workspaceinfo9 + i + ": ");
                        stringBuilder.Append(m_workspace.Datasources[i].Alias);
                        stringBuilder.Append(System.Environment.NewLine);
                    }
                }
                else
                {
                    stringBuilder.Append(m_workspaceinfo10);
                    stringBuilder.Append(System.Environment.NewLine);
                }
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append("************************************************************");
                stringBuilder.Append(System.Environment.NewLine);
                stringBuilder.Append(m_workspaceinfo11);

                Int32 mapCount = m_workspace.Maps.Count;
                if (mapCount > 0)
                {
                    for (int i = 0; i < mapCount; i++)
                    {
                        stringBuilder.Append(m_workspaceinfo12 + i + ": ");
                        stringBuilder.Append(m_workspace.Maps[i]);
                        stringBuilder.Append(System.Environment.NewLine);
                    }
                }
                else
                {
                    stringBuilder.Append(m_workspaceinfo13);
                    stringBuilder.Append(System.Environment.NewLine);
                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return stringBuilder.ToString();

        }

        /// <summary>
        /// 创建一个节点 同时设置节点的text,imageKey,selectImageKey
        /// Create a node and set its properties of text, imageKey, selectImageKey
        /// </summary>       
        private TreeNode CreateNode(String nodeText, String imageKey, String nodeName)
        {
            TreeNode node = new TreeNode(nodeText);
            node.ImageKey = imageKey;
            node.SelectedImageKey = imageKey;
            node.Name = nodeName;
            return node;
        }
        /// <summary>
        /// 获取数据源子节点
        /// Gets the node of Datasources
        /// </summary>
        private void SetDatasourcesNode(TreeNode datasourcesNode, Datasources datasources)
        {
            try
            {
                if (datasources.Count != 0)
                {
                    for (int i = 0; i < datasources.Count; i++)
                    {
                        TreeNode sourceNode = this.CreateNode(datasources[i].Alias, "Datasource", "Datasource");
                        sourceNode.Tag = datasources[i];
                        this.SetDatsourceNode(sourceNode, datasources[i]);

                        datasourcesNode.Nodes.Add(sourceNode);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 添加遍历数据源下的数据集 将他们变成节点 加入sourcesNode之下
        /// Add the datasets get from the datasource into the node of sourcesNode
        /// </summary>
        /// <param name="sourcesNode">数据源的根节点 The root node of datasource</param>
        /// <param name="datasource">要添加的数据源 The datasource needed to be added in</param>
        private void SetDatsourceNode(TreeNode sourceNode, Datasource datasource)
        {
            try
            {
                Datasets datasets = datasource.Datasets;

                foreach (Dataset dataset in datasets)
                {
                    TreeNode datasetNode = this.CreateNode(dataset.Name, GetDatasetTypeImg(dataset), "Dataset");
                    datasetNode.Tag = dataset;
                    SetDatasetNode(datasetNode, dataset);
                    sourceNode.Nodes.Add(datasetNode);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 遍历数据集 添加数据集节点
        /// Traverse the datasets and add the nodes of datasets
        /// </summary>
        /// <param name="datasetsNode">数据集根节点 The root node of datasets</param>
        /// <param name="dataset">要添加的数据集 The datasource needed to be added in</param>     
        private void SetDatasetNode(TreeNode datasetNode, Dataset dataset)
        {
            try
            {
                if (dataset.Type == DatasetType.Network)
                {
                    DatasetVector network = dataset as DatasetVector;
                    TreeNode childDatasetNode = this.CreateNode(network.ChildDataset.Name, "Dataset_Point", "Dataset");
                    childDatasetNode.Tag = network.ChildDataset;

                    datasetNode.Nodes.Add(childDatasetNode);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 检查数据集的类型 确定他们的图标
        /// Check-up the type of the datasets and select their image
        /// </summary>
        /// <param name="dataset">要检查的数据集 The dataset needed to be check-up</param>
        /// <returns>图标在imageList中的名称 The image name</returns>    
        private String GetDatasetTypeImg(Dataset dataset)
        {
            switch (dataset.Type)
            {
                case DatasetType.Region:
                    return "Dataset_Polygon";
                case DatasetType.Text:
                    return "Dataset_Text";
                case DatasetType.Line:
                    return "Dataset_Line";
                case DatasetType.Point:
                    return "Dataset_Point";
                case DatasetType.Network:
                    return "Network";
                default:
                    return "Adorn";
            }
        }
        /// <summary>
        /// 获取maps子节点
        /// Gets the node of maps
        /// </summary>
        private void SetMapsNode(TreeNode mapsNode, Maps maps)
        {
            try
            {
                for (int i = 0; i < maps.Count; i++)
                {
                    TreeNode mapNode = this.CreateNode(maps[i], "Map", "Map");
                    mapNode.Tag = maps[i];
                    mapsNode.Nodes.Add(mapNode);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 获取三维场景子节点
        /// Gets the node of scenes
        /// </summary>
        private void SetScenesNode(TreeNode scenesNode, Scenes scenes)
        {
            try
            {
                for (int i = 0; i < scenes.Count; ++i)
                {
                    TreeNode sceneNode = this.CreateNode(scenes[i], "Scene", "Scene");
                    sceneNode.Tag = scenes[i];

                    scenesNode.Nodes.Add(sceneNode);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 获取布局子节点
        /// Gets the node of layouts
        /// </summary>
        private void SetLayoutsNode(TreeNode layoutsNode, Layouts layouts)
        {
            try
            {
                for (int i = 0; i < layouts.Count; ++i)
                {
                    TreeNode layoutNode = this.CreateNode(layouts[i], "Layout", "Layout");
                    layoutNode.Tag = layouts[i];

                    layoutsNode.Nodes.Add(layoutNode);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 设置资源子节点
        /// Sets the node of resources
        /// </summary>
        private void SetResourcesNode(TreeNode resourcesNode, Resources resources)
        {
            try
            {
                TreeNode pointResNode = this.CreateNode(m_pointResNode, "PointLibaray", "resource");
                pointResNode.Tag = resources.MarkerLibrary;

                TreeNode lineResNode = this.CreateNode(m_lineResNode, "LineLibaray", "resource");
                lineResNode.Tag = resources.LineLibrary;

                TreeNode fillResNode = this.CreateNode(m_fillResNode, "FillLibaray", "resource");
                fillResNode.Tag = resources.FillLibrary;

                resourcesNode.Nodes.Add(pointResNode);
                resourcesNode.Nodes.Add(lineResNode);
                resourcesNode.Nodes.Add(fillResNode);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 获取数据源中所有数据集
        /// Get the names of all the datasets from the datasource
        /// </summary>
        public Dictionary<string , Dataset> GetDatasetsNames()
        {
            try
            {
                if (m_workspace.Datasources.Count > 0)
                {
                    Datasource m_datasource = m_workspace.Datasources[0];
                    if (m_datasource != null)
                    {
                        //String[] datasetNames = new String[m_datasource.Datasets.Count];
                        Dictionary<string, Dataset> datasetDic = new Dictionary<string, Dataset>();
                        //Int32 index = 0;
                        foreach (Dataset dataset in m_datasource.Datasets)
                        {
                            //datasetNames[index++] = dataset.Name;
                            datasetDic.Add(dataset.Name, dataset);
                        }
                        //return datasetNames;
                        return datasetDic;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            return null;
        }

        public SceneExtend CreateScene(string sceneName)
        {
            m_workspace.Scenes.Add(sceneName, m_scenecontrol.Scene.ToXML());    //为工作空间添加场景
            OpenScene(sceneName);
            return m_scenecontrol;
        }
        public void OpenScene(string sceneNmae)
        {
            try
            {
                FormMain.formMain.DockPanelContainerScene.Controls.Add(m_scenecontrol);
                m_scenecontrol.Scene.Workspace = m_workspace;
                FormMain.formMain.LayersControlpanel.Scene = m_scenecontrol.Scene;
                m_scenecontrol.Scene.Open(sceneNmae);
                Layer3D layer = m_scenecontrol.Scene.Layers[0];
                m_scenecontrol.Scene.EnsureVisible(layer.Bounds);
                m_scenecontrol.Scene.Refresh();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 打开工作空间中第一场景（如果场景不为空）
        /// </summary>
        public void OpenOneScene()
        {
            if (m_workspace.Scenes.Count > 0)
                OpenScene(m_workspace.Scenes[0]);
        }
        public void SceneAddDataset(Dataset dataset)
        {
            m_scenecontrol.Scene.Layers.Add(dataset, new Layer3DSettingVector(), true);
        }
        /*
        public void AddDatasetsToScene()
        {
            try
            {
                DatasetVector dataset = (DatasetVector)m_workspace.Datasources[0].Datasets["Capital"];
                Layer layer = null;

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        */
    }
}


